
from flask import Flask,request,jsonify
import json

from config import Config
#导入sqlalchmy
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)

app.config.from_object(Config)
#初始化
db = SQLAlchemy(app)

#orm方式创建表,学生表,id,name,age
class User(db.Model):
    __tablename__ = 'stu'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(80), unique=True,default='')
    age = db.Column(db.Integer,default=0)

#创建表：
# db.create_all()

@app.route('/add',methods=['POST','GET'])
def add():
    if request.method=='POST':
        stu = User(name='李四',age=19)
        db.session.add(stu)
        db.session.commit()
        return 'ok'
    else:
        ser = User.query.all()
        for i in ser:
            print(i.name,i.age)
        return 'ok'

@app.route('/update',methods=['UPDATE','DELETE'])
def update():
    if request.method=='PUT':
        User.query.filter_by(name='张三').update({'age':30})
        db.session.commit()
        return 'ok'
    else:
        pass



if __name__ == "__main__":
    app.run()
